package com.hualan.sfzy.controller.score;
import com.hualan.sfzy.entity.score.Score;
import com.hualan.sfzy.service.score.ScoreService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
public class ScoreController {

    @Autowired
    ScoreService scoreService;

    @PostMapping("/score")
    public Result add(@RequestBody Score score){
        int num = scoreService.add(score);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/score/{id}")
    public Result remove(@PathVariable Integer id){
        int num = scoreService.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/score")
    public Result removeBatch(Integer[] ids){
        int num = scoreService.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();
    }

    @PutMapping("/score")
    public Result edit(@RequestBody Score score){
        int num = scoreService.edit(score);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/score/{id}")
    public Result findById(@PathVariable Integer id){
        Score score = scoreService.findById(id);
        return Result.success(score);
    }

    @GetMapping("/score/page")
    public Result findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize,
                                         Score score){
        Map<String, Object> map = scoreService.findByPage(pageNum, pageSize, score);
        return Result.success(map);
    }

    @GetMapping("/score")
    public Result find(){
        List<Score> list = scoreService.find();
        return Result.success(list);
    }

}
